package de.g18.gruppe3.gui.controller;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import de.g18.gruppe3.common.exception.ServiceException;
import de.g18.gruppe3.gui.helper.AbstractAction;
import de.g18.gruppe3.gui.view.SQLConnectionView;

public class SqlConnectionViewController {

	private final SQLConnectionView sqlConnectionView;
	private final AbstractAction loginAction;
	private String sqlPath;
	private String sqlLogin;
	private String sqlPassword;

	public SqlConnectionViewController() {
		sqlConnectionView = new SQLConnectionView("SQL-Verbindung");
		loginAction = new UserLoginOK();

		sqlConnectionView.getbUCSqlOk().addActionListener(loginAction);
		sqlConnectionView.getbUCSqlChancel().addActionListener(new UserLoginChancel());

		sqlConnectionView.gettfUCSqlViewLogin().addKeyListener(new PressEnter());
		sqlConnectionView.gettfUCSqlConnection().addKeyListener(new PressEnter());
		sqlConnectionView.getpfCSqlViewPasswort().addKeyListener(new PressEnter());
	}

	public String getSqlPath() {
		return sqlPath;
	}

	public void setSqlPath(String sqlPath) {
		this.sqlPath = sqlPath;
	}

	public String getSqlLogin() {
		return sqlLogin;
	}

	public void setSqlLogin(String sqlLogin) {
		this.sqlLogin = sqlLogin;
	}

	public String getSqlPassword() {
		return sqlPassword;
	}

	public void setSqlPassword(String sqlPassword) {
		this.sqlPassword = sqlPassword;
	}

	private class UserLoginOK extends AbstractAction {

		@Override
		public void execute(ActionEvent arg0) throws ServiceException {
			setSqlPath(sqlConnectionView.gettfUCSqlConnection().getText());
			setSqlLogin(sqlConnectionView.gettfUCSqlViewLogin().getText());
			setSqlPassword(new String(sqlConnectionView.getpfCSqlViewPasswort().getPassword()));
			sqlConnectionView.dispose();
		}
	}

	private class UserLoginChancel extends AbstractAction {

		@Override
		public void execute(ActionEvent arg0) {
			sqlConnectionView.dispose();
		}

	}

	private class PressEnter implements KeyListener {
		@Override
		public void keyTyped(KeyEvent arg0) {

		}

		@Override
		public void keyReleased(KeyEvent arg0) {
		}

		@Override
		public void keyPressed(KeyEvent arg0) {
            if (arg0.getKeyCode() == KeyEvent.VK_ENTER) {
                loginAction.actionPerformed(null);
            }
		}

	}
}